﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace DatabaseManager.Data.Repositories
{
    public class DatabaseRepository : IDatabaseRepository
    {
        private readonly string _connectionString;
        private readonly ISqlSugarClient _db;

        public DatabaseRepository(string connectionString)
        {
            _connectionString = connectionString;
            _db = new SqlSugarClient(connectionString);

            InitializeDatabase();
        }

        private void InitializeDatabase()
        {
            // 创建必要的表
            _db.CodeFirst.InitTables<DatabaseConnection, Application, ApplicationDatabaseRelation, GeneratedClass>();
        }

        public async Task<Guid> SaveDatabaseConnectionAsync(DatabaseConnection connection)
        {
            await _db.Insertable(connection).ExecuteCommandAsync();
            return connection.Id;
        }

        public async Task<DatabaseConnection> GetDatabaseConnectionAsync(Guid id)
        {
            return await _db.Queryable<DatabaseConnection>().InSingleAsync(id);
        }

        public async Task<List<DatabaseConnection>> GetAllConnectionsAsync()
        {
            return await _db.Queryable<DatabaseConnection>().ToListAsync();
        }

        public async Task DeleteDatabaseConnectionAsync(Guid id)
        {
            await _db.Deleteable<DatabaseConnection>().InSingleAsync(id);
        }

        // 实现其他接口方法...
    }
}
